﻿using UnityEngine;
using System.Collections;
using Assets.Scripts;

public class PlayerController : MonoBehaviour {

    public const int PLAYER1_LIMIT = -15;
    public const int PLAYER2_LIMIT = 15;

	public float speed;
	public Boundary boundary;

    /// <summary>
    /// Called at the creation of the game.
    /// </summary>
	void Start(){
		if (PlayerPrefs.GetInt ("option") == 0) {
			PlayerPrefs.SetInt ("option",1);
		}
	}

    /// <summary>
    /// Called every physics steps, used for regular updates
    /// </summary>
	void FixedUpdate(){
        if (Application.loadedLevelName == "onePlayerGame")
        {
            Screen.showCursor = false;
            float moveHorizontal = 0.0f;
            float moveVertical = 0.0f;

            // set the position of the player racket depending on the option selected (mouse or keys) in 1 player mod
            if (PlayerPrefs.GetInt("option") == 1)
            {
                moveHorizontal = Input.GetAxis("Horizontal");
                moveVertical = Input.GetAxis("Vertical");
            }
            else if (PlayerPrefs.GetInt("option") == 2)
            {
                moveHorizontal = Input.GetAxis("Mouse X") * 4.5f;
                moveVertical = Input.GetAxis("Mouse Y") * 4.5f;
            }

            Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
            rigidbody.velocity = movement * speed;

            rigidbody.position = new Vector3(
                Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
                Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax),
                PLAYER1_LIMIT
                );
        }
        else if (Application.loadedLevelName == "twoPlayersGame")
        {
            // set the position of the player1 and player2 rackets

            if (this.name == "Player")
            {
                float moveHorizontal = Input.GetAxis("Horizontal");
                float moveVertical = Input.GetAxis("Vertical");

                Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
                rigidbody.velocity = movement * speed;

                rigidbody.position = new Vector3(
                    Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
                    Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax),
                    PLAYER1_LIMIT
                    );
            }

            else
            {
                float moveHorizontal2 = Input.GetAxis("Horizontal2");
                float moveVertical2 = Input.GetAxis("Vertical2");



                Vector3 movement2 = new Vector3(moveHorizontal2, moveVertical2, 0.0f);
                rigidbody.velocity = movement2 * speed;

                rigidbody.position = new Vector3(
                    Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
                    Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax),
                    PLAYER2_LIMIT
                    );
            }
        }
	} 
}
